package com.real.operationalcontrols.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ExpandableListView;
import android.widget.SearchView;

import com.google.gson.Gson;
import com.real.operationalcontrols.R;
import com.real.operationalcontrols.rxhttp.MyDialogObserver;
import com.real.operationalcontrols.rxhttp.RxHttpManger;
import com.real.operationalcontrols.企业列表.CompanyDetialActivity;
import com.real.operationalcontrols.基类.MainBaseFragment;
import com.real.operationalcontrols.实体类.WuRanYuanCompanyBean;
import com.real.operationalcontrols.适配器.CompanyAdapter;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;

public class FragmentCompany extends MainBaseFragment implements SearchView.OnQueryTextListener {
    @BindView(R.id.search_view)
    SearchView searchView;
    @BindView(R.id.rv)
    ExpandableListView rv;
    Unbinder unbinder;
    @BindView(R.id.frash)
    SmartRefreshLayout frash;
    CompanyAdapter adapter;
    List<WuRanYuanCompanyBean.FactoryBean> data = new ArrayList<>();

    @Override
    protected int getContentLayoutId() {
        return R.layout.fragment_company_layout;
    }

    @Override
    protected void loadData() {
        Map<String, String> map = new HashMap<>();
        map.put("", "");
        RxHttpManger.getInstance().get(this, "http://lulibo.club/yunwei/", map,
                new MyDialogObserver(getmContext()) {
                    @Override
                    public void onSuccess(String response) {
                        WuRanYuanCompanyBean bean = new Gson().fromJson(response,
                                WuRanYuanCompanyBean.class);
                        if (bean.getSuccess().equals("true")) {
                            data.clear();
                            data.addAll(bean.getFactory());
                            adapter.setData(data);
                        }
                    }
                });
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // TODO: inflate a fragment view
        View rootView = super.onCreateView(inflater, container, savedInstanceState);
        unbinder = ButterKnife.bind(this, rootView);
        searchView.setOnQueryTextListener(this);
        initAdapter();
        initfresh();
        return rootView;
    }

    private void initfresh() {
        frash.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(RefreshLayout refreshLayout) {
                refreshLayout.getLayout().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        loadData();
                    }
                }, 200);
                refreshLayout.finishRefresh();
            }
        });
    }

    private void initAdapter() {
        rv.setGroupIndicator(null);
        adapter = new CompanyAdapter(getmContext());
        rv.setAdapter(adapter);
        //为ListView启动过滤
        rv.setTextFilterEnabled(true);
        searchView.setSubmitButtonEnabled(false);
        rv.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {
            @Override
            public boolean onChildClick(ExpandableListView expandableListView, View view, int i,
                                        int i1, long l) {
                Intent intent = new Intent(getmContext(),
                        CompanyDetialActivity.class);
                intent.putExtra("name",
                        data.get(i).getName() + data.get(i).getDevice().get(i1).getName());
                intent.putExtra("id", data.get(i).getDevice().get(i1).getId());
                startActivity(intent);
                return true;
            }
        });
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }

    @Override
    public boolean onQueryTextSubmit(String query) {
        return false;
    }

    @Override
    public boolean onQueryTextChange(String newText) {
        //如果newText长度不为0
        if (!TextUtils.isEmpty(newText)) {
            adapter.getFilter().filter(newText.toString());//替换成本句后消失黑框！！！
        } else {
            adapter.setData(data);
        }
        return true;
    }
}
